package xyz.xtgl.config.helper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import xyz.xtgl.erp.system.entity.SysUser;
import xyz.xtgl.erp.system.entity.help.SysUserHelp;
import xyz.xtgl.erp.system.service.ISysUserService;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liubo
 * @created 2020-02-02 20:15
 */
public class MyUserDetailsService implements UserDetailsService {

    @Autowired
    private ISysUserService sysUserService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        // 1.根据数据库查询，用户是否登陆
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq(SysUserHelp.phone,username);
        SysUser user = sysUserService.getOne(wrapper);
        // 2.查询该用户信息权限
        if (user != null) {
            // 设置用户权限
            List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
            authorities.add(new SimpleGrantedAuthority("*"));
            return new MyCustomUserDetails(username,user.getPassword(),user,authorities) ;
        }
        throw new UsernameNotFoundException("用户名不存在");
    }

}
